/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael K?lling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;
        
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room cining, xianfu,yangxin,yikun, qianqing,kunning,garden,jingren,zhongcui,yanxi,door;
      
        // create the rooms
        cining = new Room("接受了任务的你现在身处慈宁宫。");
        xianfu = new Room("你来到咸福宫。", new Thing("咸福宫的贵人正在愉快的聊天，心情不错赏赐你一杯热茶。","热茶", 10));
        yangxin = new Room("你来到养心殿。",new Thing("不知道哪位贵人遗落了一个暖手炉在养心殿门口。","暖手炉", 30));
        yikun = new Room("你来到翊坤宫。", new Thing("贵妃随手赏赐你一样东西","神秘物件", -20));
        qianqing = new Room("你误闯乾清宫，被皇上下令拖出去乱棍打死。（你可以选择quit。）",new Thing("皇上正在和大臣议事","误闯死罪",-100));
        kunning = new Room("你来到坤宁宫。", new Thing("皇后正在吃甜点，心情大悦，赏赐你几块桂花糕。","桂花糕", 20));
        jingren = new Room("你来到景仁宫", new Thing("桌子上有一杯可以自取的茶。","冰茶", -30));
        garden = new Room("你来到御花园。");
        zhongcui = new Room("你来到钟粹宫。", new Thing("嫔妃正在用膳，赏赐你一碗汤。","热汤", 20));
        yanxi = new Room("你来到延禧宫。", new Thing("延禧宫贵妃心情不佳，随手向你扔来一个花瓶。","贵妃扔的花瓶", -40));
        door = new Room("你成功到达了神武门。恭喜你成功出宫为太后买到了顺福斋的红枣糕！（任务成功，晋升为大宫女。游戏结束！！！）");
        
        cining.setExit("east", yangxin);
        cining.setExit("north", xianfu);
        xianfu.setExit("east", yikun);
        xianfu.setExit("south", cining);
        yangxin.setExit("west", cining);
        yangxin.setExit("north", yikun);
        yangxin.setExit("east", qianqing);
        yikun.setExit("west", xianfu);
        yikun.setExit("south", yangxin);
        yikun.setExit("east", kunning);
        kunning.setExit("east", jingren);
        kunning.setExit("west", yikun);
        kunning.setExit("south", qianqing);
        kunning.setExit("north", garden);
        garden.setExit("east", zhongcui);
        garden.setExit("south", kunning);
        jingren.setExit("north", zhongcui);
        jingren.setExit("west", kunning);
        zhongcui.setExit("south", jingren);
        zhongcui.setExit("north", door);
        zhongcui.setExit("east", yanxi);
        zhongcui.setExit("west", garden);
        yanxi.setExit("west", zhongcui);
        
        currentRoom = cining;  // start game outside
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("太后没能吃到红枣糕，你失去了晋升机会。");
        System.out.println("谢谢体验。");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("天寒地冻，慈宁宫 口味刁钻的太后突然想吃宫外顺福斋的红枣糕。");
        System.out.println("你作为慈宁宫的小宫女接受了这个任务，需要前往神武门出宫。");
        System.out.println("新入宫的你不知如何是好，善良的老嬷嬷悄悄告诉你一神口诀“help”,据说会得到指引。");
        System.out.println();
        System.out.println("当下：" + currentRoom.getDescription());
        currentRoom.printExits();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("我不理解你想做什么。");
            return false;
        }

        Word commandWord = command.getCommandWord();
        
        switch(commandWord){
          case HELP:
             printHelp();
          break;
          case GO:
          goRoom(command);
          break;
          case QUIT:
          wantToQuit = quit(command);
          break;
          case PICK:
             pickThing();
          case EAT:
             eatThing(command);
             break;
          case CHECK:
             player.checkBag();
             break;
          case LOOK:
            lookRoom();
            break;
        }
        
        return wantToQuit;
    }

    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        
        System.out.println("天寒地冻，你每走一段路都会消耗自身生命值。");
        System.out.println("请谨慎选择你接下来要走的路，希望你能成功活着为太后买到红枣糕。");
        System.out.println();
        System.out.println("灵光乍现，你得到了神秘指引：");
        System.out.println("  go ：往哪个方向的路走");
        System.out.println("  help ：获得神秘指引");
        System.out.println("  quit : 放弃这个任务");
        System.out.println("  look ：了解当下情况");
        System.out.println("  check ：查看自己获得的物品");
        System.out.println("  pick ：接受物品");
        System.out.println("  eat ：使用小行囊里的物品");
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("你要走哪个方向的路？");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goNext(direction);

        if (nextRoom == null) {
            System.out.println("这边无路可走。");
        }
        else {
            currentRoom = nextRoom;
            System.out.println("当下：" + currentRoom.getDescription());
            currentRoom.printExits();
        }
    }
    private void lookRoom()
    {
        if(currentRoom.getThing()==null)
            System.out.println("别看了，赶紧赶路，太后着急要吃红枣糕");
        else System.out.println("你了解到："+currentRoom.getThing().getScene());
    }
    private void pickThing()
    {
        player.pick(currentRoom.getThing());
        currentRoom.remove();
    }
    private void eatThing(Command command)
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("你要使用什么东西？");
            return;
        }
        player.eat(command.getSecondWord());
    }
    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("你确定放弃这个任务吗？");
            return false;
        }
        else {
            return true;// signal that we want to quit
        }
    }
}